<template>
  <div class="yuyue-list">
    <div class="filter-container">
      <el-input v-model="filter.cheliangId" placeholder="车辆ID" style="width: 120px; margin-left: 10px" />
      <el-input v-model="filter.userId" placeholder="用户ID" style="width: 120px; margin-left: 10px" />
      <el-button type="primary" @click="fetchData" style="margin-left: 10px">
        查询
      </el-button>
    </div>

    <el-table :data="list" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" />
      <el-table-column prop="cheliangId" label="车辆ID" width="80" />
      <el-table-column prop="userId" label="用户ID" width="80" />
      <el-table-column prop="startTime" label="开始时间" width="160" />
      <el-table-column prop="endTime" label="结束时间" width="160" />
      <el-table-column label="状态" width="100">
        <template #default="{ row }">
          <el-tag :type="getStatusType(row.status)">
            {{ getStatusText(row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" />
      <el-table-column label="操作" width="220" v-if="hasPermission">
        <template #default="{ row }">
          <el-button size="small" type="danger" @click="handleCancel(row.id)"
            v-if="row.status === 0 || row.status === 1" icon="Delete" class="action-btn">
            取消
          </el-button>
          <el-button size="small" type="success" @click="handleApprove(row.id)" v-if="row.status === 0"
            icon="CircleCheck" class="action-btn">
            通过
          </el-button>
          <el-button size="small" type="warning" @click="handleReject(row.id)" v-if="row.status === 0" icon="Close"
            class="action-btn">
            拒绝
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import {
  getYuyueList,
  getYuyueByCheliangId,
  getYuyueByUserId,
  cancelYuyue,
  approveYuyue,
  rejectYuyue
} from '@/api/yuyue'
import { ElMessage } from 'element-plus'
import type { Yuyue } from '@/types/yuyue'

const list = ref<Yuyue[]>([])
const filter = reactive({
  status: undefined as number | undefined,
  cheliangId: '',
  userId: ''
})
const hasPermission = ref(true) // 根据实际权限控制

const getStatusText = (status: number) => {
  switch (status) {
    case 0:
      return '待审核'
    case 1:
      return '已通过'
    case 2:
      return '已拒绝'
    case 3:
      return '已取消'
    default:
      return '未知'
  }
}

const getStatusType = (status: number) => {
  switch (status) {
    case 0:
      return 'warning'
    case 1:
      return 'success'
    case 2:
      return 'danger'
    case 3:
      return 'info'
    default:
      return ''
  }
}

const fetchData = async () => {
  try {
    let res
    if (filter.cheliangId) {
      res = await getYuyueByCheliangId(Number(filter.cheliangId))
    } else if (filter.userId) {
      res = await getYuyueByUserId(Number(filter.userId))
    } else {
      // 确保status参数正确传递
      const params = { status: filter.status }
      res = await getYuyueList(filter.status !== undefined ? params : {})
    }
    list.value = res.data.data || []
  } catch (error) {
    ElMessage.error('获取预约列表失败')
    console.error(error)
  }
}

const handleCancel = async (id: number) => {
  try {
    await cancelYuyue(id)
    ElMessage.success('取消成功')
    fetchData()
  } catch (error) {
    ElMessage.error('取消失败')
  }
}

const handleApprove = async (id: number) => {
  try {
    await approveYuyue(id)
    ElMessage.success('审核通过')
    fetchData()
  } catch (error) {
    ElMessage.error('操作失败')
  }
}

const handleReject = async (id: number) => {
  try {
    await rejectYuyue(id)
    ElMessage.success('已拒绝')
    fetchData()
  } catch (error) {
    ElMessage.error('操作失败')
  }
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.filter-container {
  margin-bottom: 20px;
}

.action-btn {
  width: 80px;
  height: 32px;
  margin-right: 8px;
  border-radius: 4px;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  padding: 8px 12px;
}

.action-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* 确保按钮在单元格中垂直居中 */
.el-table__body-wrapper .el-table__cell {
  display: flex;
  align-items: center;
}

/* 按钮容器布局 */
.el-table-column--op .cell {
  display: flex;
  justify-content: center;
}
</style>
